package com.atguigu.service.impl;

import com.alibaba.dubbo.config.annotation.Service;
import com.atguigu.dao.DictDao;
import com.atguigu.entity.Dict;
import com.atguigu.service.DictService;
import com.base.BaseDao;
import com.base.BaseServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
@Service
public class DictServiceImpl extends BaseServiceImpl<Dict> implements DictService {
    @Autowired
    private DictDao dictDao;
    @Override
    protected BaseDao<Dict> getEntityDao() {
        return dictDao;
    }

    @Override
    public List<Map<String, Object>> findZnodes(Long parentId) {
        List<Dict> znodes = dictDao.findListByParentId(parentId);
        List<Map<String, Object>> list = new ArrayList<>();
        for(Dict dict: znodes){
            HashMap<String, Object> map = new HashMap<>();
            map.put("id",dict.getId());
            map.put("name",dict.getName());
            int count = dictDao.countIsParent(dict.getId());
            map.put("isParent",count>0? true:false );
            list.add(map);
        }
        return list;
    }

    @Override
    public List<Dict> findListByCode(String code) {
        Dict dict = dictDao.getByDictCode(code);
        List<Dict> list = dictDao.findListByParentId(dict.getId());
        return list;
    }

    @Override
    public String getNameById(Long id) {
        String name = dictDao.getNameById(id);
        return name;
    }
}
